﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using WCS.Utils;
using WCS.Helpers;
using WCS.Entity;
using WcsTask;
using WcsTask.DevCommTaks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

namespace WCS.Controls.系统查询
{
    public partial class ChangeNum : UIPage
    {
        public ChangeNum()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 数据库连接对象
        /// </summary>
        static SqlSugarDapper SqlSugarDapper = SqlSugarDapper.GetDbContext();

        private void uiButton1_Click(object sender, EventArgs e)
        {
            UIButton button = (UIButton)sender;
            button.Enabled = false;

            int last = Convert.ToInt16(button.Name.Substring(button.Name.Length - 1));

            foreach (Control ctrl in this.Controls)
            {
                var asd = ctrl.GetType().Name;
                if (ctrl.GetType().Name == "UITextBox")
                {
                    UITextBox textBox = (UITextBox)ctrl;
                    if (textBox.Name == $"Text{last}")
                    {
                        if (textBox.Text != "")
                        {
                            var num = Convert.ToInt32(textBox.Text);
                            if (num > 10)
                            {
                                button.Enabled = true;
                                return;
                            }
                            foreach (Control ctrl1 in this.Controls)
                            {
                                var type = ctrl1.GetType().Name;
                                if (ctrl1.GetType().Name == "UILabel")
                                {
                                    Label label = (Label)ctrl1;
                                    if (label.Name == $"num{last}")
                                    {
                                        label.Text = num + "";

                                        SqlSugarDapper.DB.Updateable<WcsDevStation>()
                                            .SetColumns(s => s.MaterialNum == num)
                                            .Where(s => s.Station_Plc_No == $"100{last}").ExecuteCommand();
                                    }
                                }
                            }
                        }
                    }
                }
                string material;
                if (ctrl.GetType().Name == "UIComboBox")
                {
                    UIComboBox textBox = (UIComboBox)ctrl;
                    if (textBox.Name == $"MaterialType{last}")
                    {
                        if (textBox.SelectedValue.ToString() != "")
                        {
                            material = textBox.Text;
                            foreach (Control ctrl1 in this.Controls)
                            {
                                var type = ctrl1.GetType().Name;
                                if (ctrl1.GetType().Name == "UILabel")
                                {
                                    Label label = (Label)ctrl1;
                                    if (label.Name == $"M{last}")
                                    {
                                        label.Text = material;

                                        var stationBuffer = $"100{last + 4}";

                                        var plcStation = SqlSugarDapper.DB.Queryable<WcsDevStation>().Where(w => w.Station_Plc_No == $"100{last}").First();

                                        SqlSugarDapper.DB.Updateable<WcsDevStation>()
                                            .SetColumns(s => s.ProductionMaterials == material)
                                            .Where(s => s.Station_Plc_No == $"100{last}" || s.Station_Plc_No == stationBuffer).ExecuteCommand();

                                        // 判断当前砂芯的小车托盘是否和产品需要的的托盘一致
                                        //if (plcStation.ProductionMaterials != material)
                                        //{
                                        //    //把小车拉走 并换上需要的托盘
                                        //    AGVTask._agvTask.CarIn(plcStation.Station_Plc_No, material);
                                        //    //不继续生产就把砂芯缓存的入库
                                        //    var station = (Convert.ToInt32(plcStation.Station_Plc_No) + 4).ToString();
                                        //    AGVTask._agvTask.CarIn(station, material);
                                        //}
                                    }
                                }
                            }
                        }
                    }
                }
            }

            button.Enabled = true;

        }

        private void AddTask_Load(object sender, EventArgs e)
        {
            Dictionary<string, string> PLCType = new Dictionary<string, string>();
            foreach (var item in SqlSugarDapper.DB.Queryable<Entity.PalletToMaterial>().ToList().FindAll(f => !f.MaterialType.Contains("物料")))
            {
                PLCType.Add(item.MaterialType, item.MaterialType);
            }

            MaterialType1.DataSource = new BindingSource(PLCType, null);
            MaterialType1.DisplayMember = "key";
            MaterialType1.ValueMember = "value";

            MaterialType2.DataSource = new BindingSource(PLCType, null);
            MaterialType2.DisplayMember = "key";
            MaterialType2.ValueMember = "value";

            MaterialType3.DataSource = new BindingSource(PLCType, null);
            MaterialType3.DisplayMember = "key";
            MaterialType3.ValueMember = "value";

            MaterialType4.DataSource = new BindingSource(PLCType, null);
            MaterialType4.DisplayMember = "key";
            MaterialType4.ValueMember = "value";

            foreach (var item in SqlSugarDapper.DB.Queryable<WcsDevStation>().ToList())
            {
                if (item.Station_Plc_No == "1001")
                {
                    MaterialType1.Text = item.ProductionMaterials;
                    M1.Text = item.ProductionMaterials;
                }
                if (item.Station_Plc_No == "1002")
                {
                    MaterialType2.Text = item.ProductionMaterials;
                    M2.Text = item.ProductionMaterials;
                }
                if (item.Station_Plc_No == "1003")
                {
                    MaterialType3.Text = item.ProductionMaterials;
                    M3.Text = item.ProductionMaterials;
                }
                if (item.Station_Plc_No == "1004")
                {
                    MaterialType4.Text = item.ProductionMaterials;
                    M4.Text = item.ProductionMaterials;
                }
            }
        }
    }
}
